<?php
// +----------------------------------------------------------------------
// | makePHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------
/**
 *  Entrance File of the Control class.
 *
 * @Author:    <>
 * @Package:   Interface of the View class. 
 * @Version:   $Id: zy_materials_from.class.php 2011-07-11 11:22:52 $
 * @Show Note: 
 */
Class zy_materials_from extends TheAdmin
{
	/* DataBase object */
	public $db;

	/* View the data page limit items */
	public $pageSize = PAGENO;
	
	/**
	 *	Construct this module class.
	*/
	public function zy_materials_from()
	{
	}
	public function set()
	{
		$this->TheAdmin();
		//定义模版
		$this->tplFile ="zy_materials_from.tpl";
		$this->admin_user_id = $this->userRequest->get('admin_user_id');
		//取得数据库连接
		$this->db = Factory::getDB("zy_materials_from");
		//取得用户对于该类的权限
		$user_p = $this->get_user_popedom_by_class_name(__CLASS__);
		$class_p_arr = $this->show_p();
		//取得该类的权值数组
		$class_method = $class_p_arr['class_method'];
		//取得用户操作菜单
		$menu = array();
		foreach ($class_method as $k=>$v)
		{
			if( (parse_power($user_p,$v[2]) && (isset($v[3]) ?$v[3] :false)) || IS_ROOT)
			{
				array_push($menu,array('url'=>"?c=".__CLASS__."&m={$v[0]}",'title'=>$v[1]));
			}
		}
		//用户有权菜单生成 可以添加其他操作
		$this->smarty->assign('menus',$menu);
		$this->smarty->assign('pay_type_arr',get_pay_type_arr());//付款方式
		$this->smarty->assign('transport_type_arr',get_transport_type());//运输方式
		$this->smarty->assign("main_menus",$this->get_user_menus());
	}
	
	/**
	 *	Call class construct method.
	*/
	public function __construct()
	{
			$this->zy_materials_from();
	}
	public function _display()
	{
		$this->smarty->display($this->tplFile);
	}
	/**
	 *	default method.
	*/
	public function show()
	{
		$col = "materials_from_id";
		$where = 1;
		//自购单号检索
		$materials_no = isset($_GET['materials_no']) ? trim($_GET['materials_no']) : '';
		if (!empty($materials_no)) {
			$where .= " AND materials_no = '{$materials_no}' ";
		}
		//申请人检索
		$admin_user_id = isset($_GET['admin_user_id']) ? trim($_GET['admin_user_id']) : '';
		if (!empty($admin_user_id)) {
			$where .= " AND admin_user_id = {$admin_user_id} ";
		}
		$rowNo = $this->db->getSumNumber($this->db->tableName,$where);
		//数据库执行sql语句出错
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		if( 0 > $rowNo) goToIndex($rowNo);
		//分页
		$page = Factory::getBaseClass('pagination');
		$page->set($rowNo,$this->pageSize);
		$numrows = $this->pageSize;
		$offset  = $page->getSqlMove();
		$arr = $this->db->getAll($where,$col,$numrows,$offset);
		admin_log($this->admin_user_id,__METHOD__,'查看',1);
		//数据库执行select语句出错
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		if(0 > $arr ) goToIndex($arr);
		//自购物资具体数据
		/*$materials_arr = array();
		foreach ($arr as $value)
		{
			$materials_arr[$value['materials_from_id']] = $this->get_materials_info($value['materials_from_id']);//根据自购物资ID取得具体信息
		}*/
		
		/*$this->smarty->assign('materials_arr',$materials_arr);*/
		$this->smarty->assign('arr',$arr);
		//默认方法
		$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
		$this->smarty->assign('act','list');
		$this->smarty->display($this->tplFile);
	}
	
	/**
	 *	add method.
	*/
	public function add()
	{
		//添加数据
		if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
		{
			//使用模版，展示提交界面
			$this->smarty->assign('act_title','添加');
			$this->smarty->assign('act','add');
			$this->smarty->display($this->tplFile);
		} else {
			//用户提交，把数据插入数据库
			$temp = $this->getValue();
			$insrt = $this->db->insert_zy_materials_from($temp);
			$error_no = 0 > $insrt ?  $insrt :'-1501';
			admin_log($this->admin_user_id,__METHOD__,"id={$insrt}",2);
			//取得错误的sql语句
			//$error_sql = end($this->db->getDebugMsg());
			//echo $error_sql;
			if ($insrt < 0) {
				alertMsg($error_no, 'index.php?c=zy_materials_from');
			}else{
				redirectPage('index.php?c=zy_materials_from');
			}
			//alertMsg($error_no, 'index.php?c=zy_materials_from');
		}
	}
	
	/**
	 *	edit method.
	*/
	public function edit()
	{
		//取得id，也就是唯一索引
		$materials_from_id = isset($_REQUEST['materials_from_id']) ? intval(trim($_REQUEST['materials_from_id'])) : 0 ;
		//取得是否查看详情
		$is_show = isset($_REQUEST['is_show']) ? intval(trim($_REQUEST['is_show'])) : 0 ;
		if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
		{
			//使用模版，展示提交界面
			$result = $this->db->getOne($materials_from_id);
			if(empty($result))
			{
				alertMsg('-2001', 'index.php?c=zy_materials_from');
			}
			foreach ($result AS $k=>$v)
			{
				$this->smarty->assign("{$k}",$v);
			}
			$this->smarty->assign('is_show',$is_show);
			$this->smarty->assign('act_title','修改');
			$this->smarty->assign('act','edit');
			$this->smarty->display($this->tplFile);
		} else {
			//用户提交，把数据插入数据库
			//设置where条件
			$where = "materials_from_id=$materials_from_id";
			$temp = $this->getValue(1);
			$update = $this->db->update_zy_materials_from($temp,$where);
			admin_log($this->admin_user_id,__METHOD__,$where,2);
			$error_no = 0 > $update ?  $update :'-1502';
			//取得错误的sql语句
			//$error_sql = end($this->db->getDebugMsg());
			//echo $error_sql;
			if ($update < 0) {
				alertMsg($error_no, 'index.php?c=zy_materials_from');
			}else{
				redirectPage('index.php?c=zy_materials_from');
			}
			//alertMsg($error_no, 'index.php?c=zy_materials_from');
		}
	}
	
	/**
	 *	browse method.
	*/
	public function browse() {
		$materials_from_id = isset($_REQUEST['materials_from_id']) ? intval(trim($_REQUEST['materials_from_id'])) : 0 ;
		//使用模版，展示提交界面
		$result = $this->db->getOne($materials_from_id);
		if(empty($result))
		{
			alertMsg('-2001', 'index.php?c=zy_materials_from');
		}
		foreach ($result AS $k=>$v)
		{
			$this->smarty->assign("{$k}",$v);
		}
		$materials_arr = $this->get_materials_info($materials_from_id);//根据自购物资ID取得具体信息
		//$this->smarty->assign('is_show',$is_show);
		$this->smarty->assign('materials_arr',$materials_arr);
		$this->smarty->assign('act_title','查看');
		$this->smarty->assign('act','browse');
		$this->smarty->display($this->tplFile);
	}
	
	/**
	 *	del method.
	*/
	public function del()
	{
		//取得id，也就是唯一索引
		$materials_from_id = isset($_REQUEST['materials_from_id']) ? intval(trim($_REQUEST['materials_from_id'])) : 0 ;
		//检测是否存在详细 如果存在 提示请先删除详细 在删除这条数据
		$temp = $this->get_materials_info($materials_from_id);
		if (!empty($temp)) {
			alertMsg('-5006');
		}
		$where = "materials_from_id=$materials_from_id";
		$del=$this->db->delete_zy_materials_from($where);
		admin_log($this->admin_user_id,__METHOD__,$where,3);
		$error_no = 0 > $del ?  $del :'-1503';
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		if ($del < 0) {
			alertMsg($error_no, 'index.php?c=zy_materials_from');
		}else{
			redirectPage('index.php?c=zy_materials_from');
		}
		//alertMsg($error_no, 'index.php?c=zy_materials_from');
	}
	
	/**
	 *	get request method.
	*/
	public function &getValue($isEdit=0)
	{
		$temp = array();
		$check = Factory::getBaseClass("checkBase");
		if($isEdit){
			$temp['materials_from_id'] = $_POST['materials_from_id'];
		} else {
			$temp['materials_from_id'] = "";
		}
		//补充其他的字段
		$temp['materials_no'] = isset($_POST['materials_no']) ? htmlspecialchars(trim($_POST['materials_no'])) : '';//自购单号
		if (empty($temp['materials_no'])) {
			alertMsg('-5000');
		}
		//查重复
		if ($this->check_materials_no($temp['materials_no'],$temp['materials_from_id']) == 1) {
			alertMsg('-5001');
		}
		$temp['budget_amount'] = isset($_POST['budget_amount']) ? sprintf("%01.2f",$_POST['budget_amount']) : 0;//预算金额
		$temp['actual_amount'] = isset($_POST['actual_amount']) ? sprintf("%01.2f",$_POST['actual_amount']) : 0;//实际金额
		$temp['materials_time'] = isset($_POST['materials_time']) && !empty($_POST['materials_time']) ? strtotime($_POST['materials_time']) : time();//经办日期
		$temp['materials_info'] = isset($_POST['materials_info']) ? htmlspecialchars(trim($_POST['materials_info'])) : '';//备注
		$temp['admin_user_id'] = isset($_POST['admin_user_id']) ? intval($_POST['admin_user_id']) : 0;//经办人
		$file_path = isset($_FILES["file_path"]) ? $_FILES["file_path"]: '';//附件名称
		//判断是否存在文件上传
		if (!empty($file_path['name']) && !empty($temp['materials_no'])) {
			$re = upload_file($_FILES,'file_path',$temp['materials_no']);
			$temp['file_path'] = $re;//获得上传文件名称
		}
		admin_log($this->admin_user_id,__METHOD__,serialize($temp),2);	
		return $temp;
	}
	/**
	 *	get class method and popedom.
	*/
	public function show_p()
	{
		//该方法为，提供用户使用方法定义，非web用户权限相关不用写
		return array(
			/* 定义该类中文名称*/
			'class_name'=>array(__CLASS__,'自购物资管理'),
			'class_method' =>array(
				//array（方法名，中文解释，权值,是否为菜单）
				'show'  => array('show','列表',1,true),
				'add'  => array('add','添加',2,true),
				'edit'  => array('edit','编辑',4,false),
				'del'  => array('del','删除',8,false),
				'browse' => array('browse', '查看', 16, false),
				'show_p'=> array('show_p','',0,false)
			)
		);
	}
	/**
	 * 检测自购单号是否重复
	 *
	 * @param unknown_type $materials_no 自购单号
	 * @param unknown_type $materials_from_id  自购ID
	 * @return unknown  1表示已经存在 0 表示不存在
	 */
	function check_materials_no($materials_no,$materials_from_id = '')
	{
		$zy_materials_from_db = Factory::getDB('zy_materials_from');
		return $zy_materials_from_db->check_materials_no_repeat($materials_no,$materials_from_id);
	}
	/**
	 * 根据自购ID取得具体信息
	 *
	 * @param unknown_type $materials_from_id
	 * @return unknown
	 */
	function get_materials_info($materials_from_id)
	{
		$zy_materials_from_list_db = Factory::getDB('zy_materials_from_list');
		return $zy_materials_from_list_db->get_materials_info($materials_from_id);
	}
}

?>